package cn.tedu.boot.ers.service.impl;

import cn.tedu.boot.ers.entity.OrderProduct;
import cn.tedu.boot.ers.entity.Product;
import cn.tedu.boot.ers.exception.ServiceException;
import cn.tedu.boot.ers.mapper.OrderProductMapper;
import cn.tedu.boot.ers.mapper.ProductMapper;
import cn.tedu.boot.ers.pojo.dto.OrderProductAddDTO;
import cn.tedu.boot.ers.pojo.vo.OrderProductSimpleVO;
import cn.tedu.boot.ers.pojo.vo.ProductVO;
import cn.tedu.boot.ers.service.IOrderProductService;
import cn.tedu.boot.ers.web.State;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderProductServiceImpl implements IOrderProductService {
    @Autowired
    OrderProductMapper mapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public void insert(OrderProductAddDTO orderProduct01AddDTO) {
        OrderProduct orderProduct = new OrderProduct();
        BeanUtils.copyProperties(orderProduct01AddDTO, orderProduct);
        int rows = mapper.insert(orderProduct);
        if (rows != 1) {
            throw new ServiceException(State.ERR_INSERT, "服务器正忙请稍后再试");
        }
    }

    @Override
    public void deleteProductById(Long id) {
        OrderProductSimpleVO orderProductSimpleVO = mapper.getOrderProductSimpleVO(id);
        Long idRes = orderProductSimpleVO.getId();
        int rows = mapper.deleteProductById(idRes);
        if (rows !=1 ) {
            throw new ServiceException(State.ERR_BAD_REQUEST,"删除失败,服务器忙,请稍后再试");
        }
    }


}
